package controller

import (
	"ahutoj/web/io/constanct"
	"ahutoj/web/io/request"
	"ahutoj/web/io/response"
	"ahutoj/web/utils"
	"strconv"

	"ahutoj/web/logic"

	"github.com/gin-gonic/gin"
	"github.com/gin-gonic/gin/binding"
)

// @Summary	添加提交
// @Description
// @Tags		Submit
// @Accept		json
// @Produce	json
// @Param		AddSubmitReq	body		request.AddSubmitReq	true	"-"
// @Success	200				{object}	response.AddSubmitResp
// @Router		/api/submit/commit/ [post]
func AddCommit(ctx *gin.Context) {
	logger := utils.GetLogInstance()
	req := new(request.AddSubmitReq)
	if err := ctx.ShouldBindWith(req, binding.JSON); err != nil {
		logger.Errorf("call ShouldBindWith failed, err = %s", err.Error())
		response.ResponseError(ctx, constanct.InvalidParamCode)
		return
	}

	resp, err := logic.AddSubmit(ctx, req)
	if err != nil {
		logger.Errorf("call AddSubmit failed, req=%+v, err=%s", utils.Sdump(req), err)
		response.ResponseError(ctx, constanct.ServerErrorCode)
		return
	}
	response.ResponseOK(ctx, resp)
}

// @Summary	重新判题
// @Description
// @Tags		Submit
// @Accept		json
// @Produce	json
// @Param		RejudgeSubmitReq	body		request.RejudgeSubmitReq	true	"-"
// @Success	200					{object}	response.Response
// @Router		/api/submit/rejudge/ [post]
func RejudgeCommit(ctx *gin.Context) {
	logger := utils.GetLogInstance()
	req := new(request.RejudgeSubmitReq)
	if err := ctx.ShouldBindWith(req, binding.JSON); err != nil {
		logger.Errorf("call ShouldBindWith failed, err = %s", err.Error())
		response.ResponseError(ctx, constanct.InvalidParamCode)
		return
	}

	resp, err := logic.RejudgeSubmit(ctx, req)
	if err != nil {
		logger.Errorf("call RejudgeSubmit failed, req=%+v, err=%s", utils.Sdump(req), err)
		response.ResponseError(ctx, constanct.ServerErrorCode)
		return
	}
	response.ResponseOK(ctx, resp)
}

// @Summary	重新判题个数
// @Description
// @Tags		Submit
// @Accept		json
// @Produce	json
// @Param		RejudgeSubmitReq	body		request.RejudgeSubmitReq	true	"-"
// @Success	200					{object}	response.RejudgeSubmitCountResp
// @Router		/api/submit/rejudge/count/ [post]
func RejudgeCommitCount(ctx *gin.Context) {
	logger := utils.GetLogInstance()
	req := new(request.RejudgeSubmitReq)
	if err := ctx.ShouldBindWith(req, binding.JSON); err != nil {
		logger.Errorf("call ShouldBindWith failed, err = %s", err.Error())
		response.ResponseError(ctx, constanct.InvalidParamCode)
		return
	}

	resp, err := logic.RejudgeSubmitCount(ctx, req)
	if err != nil {
		logger.Errorf("call RejudgeSubmitCount failed, req=%+v, err=%s", utils.Sdump(req), err)
		response.ResponseError(ctx, constanct.ServerErrorCode)
		return
	}
	response.ResponseOK(ctx, resp)
}

// @Summary	提交状态列表
// @Description
// @Tags		Submit
// @Accept		json
// @Produce	json
// @Param		GetListReq	body		request.GetListReq	true	"-"
// @Param		PID	query		string	true	"-"
// @Param		UID	query		string	true	"-"
// @Param		CID	query		constanct.OJResult	true	"-"
// @Param		Lang	query	constanct.LANG	true	"-"
// @Success	200				{object}	response.SubmitListResp
// @Router		/api/submit/status [get]
func StatusList(ctx *gin.Context) {
	logger := utils.GetLogInstance()
	req := new(request.SubmitListReq)
	if err := ctx.ShouldBindWith(req, binding.Query); err != nil {
		logger.Errorf("call ShouldBindWith failed, err = %s", err.Error())
		response.ResponseError(ctx, constanct.InvalidParamCode)
		return
	}

	resp, err := logic.GetSubmits(ctx, req)
	if err != nil {
		logger.Errorf("call GetSubmitList failed, req=%+v, err=%s", utils.Sdump(req), err)
		response.ResponseError(ctx, constanct.ServerErrorCode)
		return
	}
	response.ResponseOK(ctx, resp)
}

// @Summary	获取用户提交
// @Description
// @Tags		Submit
// @Accept		json
// @Produce	json
// @Param		id	path		int	true	"SID"
// @Success	200	{object}	response.GetSubmitResp
// @Router		/api/submit/{id} [get]
func GetCommit(ctx *gin.Context) {
	logger := utils.GetLogInstance()
	var err error
	req := new(request.GetSubmitReq)
	cidStr := ctx.Param("id")
	req.SID, err = strconv.ParseInt(cidStr, 10, 64)
	if err != nil {
		logger.Errorf("call ShouldBindWith failed, err = %s", err.Error())
		response.ResponseError(ctx, constanct.InvalidParamCode)
		return
	}

	resp, err := logic.GetSubmit(ctx, req)
	if err != nil {
		logger.Errorf("call GetSubmit failed, req=%+v, err=%s", utils.Sdump(req), err)
		response.ResponseError(ctx, constanct.ServerErrorCode)
		return
	}
	response.ResponseOK(ctx, resp)
}
